import axios from "axios";
import Papa from "papaparse";
import { AccountBookData } from "./type";

axios.defaults.baseURL = "http://localhost:3000";

export const getOriginData = (): Promise<AccountBookData[]> => {
  return new Promise((resolve, reject) => {
    const originData = localStorage.getItem("originData");
    // 如果本地有原始数据直接返回
    if (originData) {
      return resolve(JSON.parse(originData));
    }
    axios
      .get("/static/bill.csv")
      .then((res) => {
        const { data } = Papa.parse(res.data, {
          header: true,
        });
        resolve(data as AccountBookData[]);
      })
      .catch((e) => reject(e));
  });
};
